package br.com.cotec.dao.impl;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Query;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import br.com.cotec.dao.PosicaoDao;
import br.com.cotec.model.Posicao;

public class PosicaoDaoImpl extends GenericDaoImpl<Posicao, Serializable> implements PosicaoDao{
	/**
	 * {@inheritDoc}
	 */
	public Long ultimaPosicaoFileira(Posicao posicao) {
		String hql = "SELECT MAX(posicao.posicao) " +
		  			 "FROM Posicao posicao INNER JOIN posicao.fileira fileira " +
		  			 "INNER JOIN fileira.sala sala " +
		  			 "WHERE sala =:sala AND " +
		  			 "fileira =:fileira";
		
		Query query = (Query)em.createQuery(hql);
		query.setParameter("sala", posicao.getFileira().getSala());
		query.setParameter("fileira", posicao.getFileira());
		try {
			return (Long)query.getSingleResult();
		} catch(Exception e){
			return null;
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.REQUIRED)
	public List<Posicao> getPosicaoBySalaFileiraSortFileiraAndPosicao(Posicao posicao, Boolean exibirPosicaoUtilizada) {
		Session hibernateSession = (Session) em.getDelegate();
		Criteria criteria = hibernateSession.createCriteria(Posicao.class, "pos").add(Example.create(posicao));
		if (!exibirPosicaoUtilizada) {
			criteria.add(Restrictions.isNull("pos.equipamento"));
		}
		criteria.createCriteria("pos.fileira", "fil", Criteria.INNER_JOIN).add(Example.create(posicao.getFileira()));
		criteria.createCriteria("fil.sala", Criteria.INNER_JOIN).add(Example.create(posicao.getFileira().getSala()));

		return (List<Posicao>)criteria.list();
	}
	
	/**
	 * {@inheritDoc}
	 * 
	 * @see br.com.cotec.dao.impl.GenericDaoImpl#hasChanged(java.lang.Object)
	 */
	@Override
	public boolean hasChanged(Posicao object) {
		List<Posicao> obj = getByExample(object);
		if (obj == null || obj.size() == 0) {
			return false;
		} else {
			return true;
		}
	}
	
	/**
	 * {@inheritDoc}
	 * 
	 * @see br.com.cotec.dao.impl.GenericDaoImpl#getByExample(java.lang.Object)
	 */
	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public List<Posicao> getByExample(Posicao posicao) {
		Session hibernateSession = (Session) em.getDelegate();
		Criteria criteria = hibernateSession.createCriteria(Posicao.class, "pos").add(Example.create(posicao));
		if (posicao.getEquipamento() != null) {
			criteria.createCriteria("pos.equipamento", Criteria.INNER_JOIN).add(Example.create(posicao.getEquipamento()));
		}
		if (posicao.getFileira() != null) {
			criteria.createCriteria("pos.fileira", "fil", Criteria.INNER_JOIN).add(Example.create(posicao.getFileira()));
			if (posicao.getFileira().getSala() != null) {
				criteria.createCriteria("fil.sala", Criteria.INNER_JOIN).add(Example.create(posicao.getFileira().getSala()));
			}
		}
		return (List<Posicao>)criteria.list();
	}

}